<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

class Order extends Backend
{
    protected $model = null;

    // 关联查询
    protected $relationSearch = true;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();

        // 引入模型
        $this->model = model('Subject.Order');
    }

    // 列表
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with(['subject', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->with(['subject', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    // 删除
    public function del($ids = null)
    {
        $list = $this->model->select($ids);

        if (!$list) {
            $this->error(__('No Results were found'));
            exit;
        }

        $result = $this->model->destroy($ids);

        if ($result === FALSE) {
            $this->error($this->model->getError());
            exit;
        } else {
            $this->success();
            exit;
        }
    }
}
